"""添加ExcelConfig的has_header字段

Revision ID: 4b8e1a3f9c2d
Revises: 31434f7add_creator_field
Create Date: 2025-09-12 11:45:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '4b8e1a3f9c2d'
down_revision: Union[str, Sequence[str], None] = '31434f7add_creator_field'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # 添加has_header字段到excel_config表
    op.add_column('excel_config', sa.Column('has_header', sa.Boolean(), nullable=True, comment='是否包含标题行'))
    
    # 更新现有记录的默认值
    op.execute("""
        UPDATE excel_config 
        SET has_header = CASE 
            WHEN header_row = 1 THEN true 
            ELSE false 
        END
        WHERE has_header IS NULL
    """)
    
    # 设置字段为非空并设置默认值
    op.alter_column('excel_config', 'has_header', 
                   nullable=False,
                   server_default=sa.text('true'))


def downgrade() -> None:
    """Downgrade schema."""
    # 删除has_header字段
    op.drop_column('excel_config', 'has_header')